<template>
  <div >
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 19px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加管理员</el-breadcrumb-item>
    </el-breadcrumb>
  <el-divider></el-divider>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input  v-model="ruleForm.phoneNumber"></el-input>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input  v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <el-input  v-model="ruleForm.avatar"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="avatar">
        <el-input  v-model="ruleForm.description"></el-input>
      </el-form-item>

   <el-form-item label="是否启用" prop="choose">
     <el-switch
         v-model="ruleForm.enable"
         active-color="#13ce66"
         :active-value="1"
         :inactive-value="0"
         inactive-color="#aaaa">
     </el-switch>
   </el-form-item>


      <el-form-item label="角色选择" prop="roleIds">
        <el-select v-model="ruleForm.roleIds" multiple placeholder="请选择">
          <el-option
              v-for="item in roleListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item >
        <el-button  type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {

        username: '',
        password: '',
        nickname:'',
        phoneNumber:'',
        email:'',
        avatar: '',
        enable:'',
        description:'',
        roleIds:[],

      },

      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '长度在 11 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入简介', trigger: 'blur' },
          { min: 3, max: 100, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        roleIds: [
            {required: true, message: '请至少选择一个角色',trigger: 'blur'}
        ],

      },
      roleListOptions: [],
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9091/admins/add-new';
          console.log('url='+url);

          let formDate = this.qs.stringify(this.ruleForm)
          console.log('formDate='+formDate);
          this.axios.post(url,formDate).then((response) => {
            let responseBody = response.data;
            console.log(responseBody)
            if(responseBody.state==20000){
              //成功
              let message="添加管理员成功"
              this.$message({
                message: message,
                type: "success"

              });
              this.resetForm(formName);
            }else {
              //失败
              this.$message.error(responseBody.message)
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadRoleList(){
      let url = 'http://localhost:9091/roles';
      console.log('url='+url)
      this.axios.get(url).then((response) => {
        let responseBody = response.data
      this.roleListOptions = responseBody.data
      })
    }
  },
  mounted() {
    this.loadRoleList();
  }
}

</script>